<?php

class plugin_erling_account_center {

    public function __construct() {

    }

    public function logging_top() {
        global $_G;
        
        
        return '';
    }

    public function global_login_extra() {
        static $executed = false;
        if($executed) return '';
        $executed = true;

        $table_connfig = C::t('#erling_account_center#er_account_config');
        $table_connfig_data = $table_connfig->fetch_by_keys(['only_acount_auth','appid','redirect_url']);
        $only_acount_auth = $table_connfig_data['only_acount_auth']['value'];

        $html = '';

        // 隐藏默认登录表单
        if($only_acount_auth == 1) {
            $html .=  <<<EOF
                <script>
                document.addEventListener('DOMContentLoaded', function() {
                    var lsform = document.getElementById('lsform');
                    if(lsform) {
                        var pnsDiv = lsform.querySelector('div.pns');
                        if(pnsDiv) {
                            pnsDiv.style.display = 'none';
                        }
                    }
                });
                </script>
                EOF;
        }
        $appid = $table_connfig_data['appid']['value'];
        $domain = $table_connfig_data['redirect_url']['value'];
        $redirect_url = $domain.'/plugin.php?id=erling_account_center:redirect_callback';
        $state = md5(uniqid(mt_rand(), true));
            
        $http_login = 'https://user.20idc.com/authorize/login?type=login&' . 'domain='.$domain.'&appid='.$appid.'&redirect_url='.$redirect_url.'&response_type=code&scope=get_user_info&state='.$state;
        $http_register = 'https://user.20idc.com/authorize/register?type=register&' . 'domain='.$domain.'&appid='.$appid.'&redirect_url='.$redirect_url.'&response_type=code&scope=get_user_info&state='.$state;

        $html .= <<<EOF
            <div style="margin-bottom: 10px;float: right;margin: 10px">
                <a href="{$http_login}" target="_blank" style="display: inline-block; padding: 4px 10px; background-color: #2870c9; color: #fff; text-decoration: none; border-radius: 4px; margin-left: 10px;">聚合账号登录</a>
                <a href="{$http_register}" target="_blank" style="display: inline-block; padding: 4px 10px; background-color: #34a853; color: #fff; text-decoration: none; border-radius: 4px; margin-left: 10px;">聚合账号注册</a>
            </div>
            EOF;

        return $html;
    }

    public function global_header() {
        
    }
}